﻿
package Cain.page
{
	import adobe.utils.CustomActions;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import Cain.XmlLoader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import Cain.LoaderList;
	import flash.text.TextField;
	/**
	 * ...
	 * @author Cain@2009 in ADCAST
	 * PageBook v1.1
	 */
	public class PageBook extends MovieClip
	{
		private var loaderlist:LoaderList;
		private var xml_url:String;
		private var p_xml:XmlLoader;
		private var url:URLRequest;
		private var now_id:int;
		private var next_b:Sprite;
		private var back_b:Sprite;
		private var next_p_width:Number;
		private var page_time:Number;
		private var pg_acc:Page;
		private var pg_num:int;
		public function PageBook(_xml_url:String, _next_p_width:Number = 50, _page_time:Number = 2 )
		{
			next_p_width = _next_p_width;
			page_time = _page_time;
			xml_url = _xml_url;
			init();
		}
		
		private function init():void
		{
			p_xml = new XmlLoader(xml_url);
			p_xml.addEventListener("xml_load_ok",xml_load_ok);
		}
		
		private function xml_load_ok(e:Event):void 
		{
			var _num:int = p_xml.xml.page.length();
			pg_num = _num
			var _arr:Array = new Array();
			for (var i:int = 0; i < _num; i++) 
			{
				_arr.push( p_xml.xml.page[i].url.toString());
			}
			loaderlist = new LoaderList(_arr);
			loaderlist.addEventListener("0", p0_init);
			dispatchEvent(new Event("xml_load_ok"));
		}
		
		private function p0_init(e:Event):void 
		{
			addChild(loaderlist.conload(0));
			now_id = 0;
			bb_init();
			next_b.buttonMode = true;
			back_b.buttonMode = true;
			next_b.addEventListener(MouseEvent.CLICK, next_page);
			back_b.addEventListener(MouseEvent.CLICK, back_page);
			var cain_textfile:TextField = new TextField();
			cain_textfile.text = "Design by 朱信曄 cainmaila@gmail.com";
			cain_textfile.x = -2000;
			addChild(cain_textfile);
		}
		
		
		
		private function bb_init():void
		{
			
			next_b = new Sprite();
			next_b.graphics.beginFill(100,0);
			next_b.graphics.drawRect( -next_p_width, 0, next_p_width, height);
			next_b.x =  width;
			addChild(next_b);
			back_b = new Sprite();
			back_b.graphics.beginFill(0, 0);
			back_b.graphics.drawRect( 0, 0, next_p_width,height);
			//addChild(back_b);
		}
		
		public function next_page(e:MouseEvent = null):Boolean
		{
			var em:Boolean = false;
			if(loaderlist.ok(now_id+1)){
				addChild(loaderlist.conload(now_id + 1));
				addChild(loaderlist.conload(now_id));
				pg_acc = new Page(loaderlist.conload(now_id), loaderlist.conload(now_id + 1),true,page_time);
				now_id++;
				pg_acc.addEventListener("end", pg_end);
				em = true;
				dispatchEvent(new Event("start"));
			}
			return em;
		}
		public function back_page(e:MouseEvent = null):Boolean
		{
			var em:Boolean = false;
			if(loaderlist.ok(now_id-1)){
				addChild(loaderlist.conload(now_id - 1));
				addChild(loaderlist.conload(now_id));
				pg_acc = new Page(loaderlist.conload(now_id), loaderlist.conload(now_id - 1),false,page_time);
				now_id--;
				pg_acc.addEventListener("end", pg_end);
				em = true;
				dispatchEvent(new Event("start"));
			}
			return em;
		}
		private function pg_end(e:Event=null):void 
		{
			pg_acc.removeEventListener("end", pg_end);
			if(now_id>0){
				addChild(back_b);
			}
			if(now_id<pg_num-1){
				addChild(next_b);
			}
			dispatchEvent(new Event("end"));
		}
		public function get _pg_num():int
		{
			return pg_num;
		}
		public function get _now_id():int
		{
			return now_id;
		}
	}
	
}